{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# TensorBoard Usage\n",
    "## 1. TensorBoard Configuration and Startup"
   ],
   "id": "bbe4b1d8b3029dc9"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-18T09:51:40.152934Z",
     "start_time": "2025-03-18T09:51:39.142262Z"
    }
   },
   "source": [
    "import os.path\n",
    "\n",
    "import torch\n",
    "import torchvision\n",
    "from torch.utils.tensorboard import SummaryWriter\n",
    "from torchvision import transforms, datasets\n",
    "\n",
    "from constants import PROJECT_ROOT\n",
    "\n",
    "writer = SummaryWriter(os.path.join(PROJECT_ROOT, 'runs'))\n"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "run with:\n",
    "```shell\n",
    "tensorboard --logdir=runs\n",
    "```"
   ],
   "id": "e952f13fbd25d7be"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. Model Structure Visualization",
   "id": "5ca0f87684ae5a8a"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T09:51:51.190849Z",
     "start_time": "2025-03-18T09:51:43.733507Z"
    }
   },
   "cell_type": "code",
   "source": [
    "transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])\n",
    "trainset = datasets.MNIST('./data', train=True, download=True, transform=transform)\n",
    "trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)\n",
    "model = torchvision.models.resnet50(False)\n",
    "# Have ResNet model take in grayscale rather than RGB\n",
    "model.conv1 = torch.nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)\n",
    "images, labels = next(iter(trainloader))\n",
    "\n",
    "grid = torchvision.utils.make_grid(images)\n",
    "writer.add_image('images', grid, 0)\n",
    "writer.add_graph(model, images)\n",
    "writer.close()"
   ],
   "id": "3654c94726f0dc7e",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/liuzhenzhou/anaconda3/envs/python313/lib/python3.13/site-packages/torchvision/models/_utils.py:135: UserWarning: Using 'weights' as positional parameter(s) is deprecated since 0.13 and may be removed in the future. Please use keyword parameter(s) instead.\n",
      "  warnings.warn(\n",
      "/Users/liuzhenzhou/anaconda3/envs/python313/lib/python3.13/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=None`.\n",
      "  warnings.warn(msg)\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. Scalar Visualization",
   "id": "dbf201327fc2271c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T09:52:08.321271Z",
     "start_time": "2025-03-18T09:52:08.292498Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import numpy as np\n",
    "\n",
    "for n_iter in range(100):\n",
    "    writer.add_scalar('Loss/train', np.random.random(), n_iter)\n",
    "    writer.add_scalar('Loss/test', np.random.random(), n_iter)\n",
    "    writer.add_scalar('Accuracy/train', np.random.random(), n_iter)\n",
    "    writer.add_scalar('Accuracy/test', np.random.random(), n_iter)"
   ],
   "id": "6fbe5cb3865a92d1",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T09:52:19.795241Z",
     "start_time": "2025-03-18T09:52:19.791979Z"
    }
   },
   "cell_type": "code",
   "source": "writer.flush()",
   "id": "5df190858f30ba9e",
   "outputs": [],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T09:52:30.293075Z",
     "start_time": "2025-03-18T09:52:30.289554Z"
    }
   },
   "cell_type": "code",
   "source": "writer.close()",
   "id": "2438d6c0e29d23f9",
   "outputs": [],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:16:15.772478Z",
     "start_time": "2025-03-18T10:16:15.749089Z"
    }
   },
   "cell_type": "code",
   "source": [
    "r = 5\n",
    "for i in range(100):\n",
    "    writer.add_scalars('run_14h', {'xsinx': i * np.sin(i / r),\n",
    "                                   'xcosx': i * np.cos(i / r),\n",
    "                                   'tanx': np.tan(i / r)}, i)\n",
    "writer.close()\n",
    "# This call adds three values to the same scalar plot with the tag\n",
    "# 'run_14h' in TensorBoard's scalar section."
   ],
   "id": "7f17e3f9db771035",
   "outputs": [],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4. Histogram Visualization",
   "id": "390944b4d7deb18"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:20:42.195182Z",
     "start_time": "2025-03-18T10:20:42.185624Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for i in range(10):\n",
    "    x = np.random.random(1000)\n",
    "    writer.add_histogram('distribution centers', x + i, i)"
   ],
   "id": "178f2b1c19a0c71b",
   "outputs": [],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 5. Image Visualization",
   "id": "a956320011f74d61"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:25:23.445478Z",
     "start_time": "2025-03-18T10:25:23.440464Z"
    }
   },
   "cell_type": "code",
   "source": [
    "img = np.zeros((3, 100, 100))\n",
    "img[0] = np.arange(0, 10000).reshape(100, 100) / 10000\n",
    "img[1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000\n",
    "\n",
    "img_HWC = np.zeros((100, 100, 3))\n",
    "img_HWC[:, :, 0] = np.arange(0, 10000).reshape(100, 100) / 10000\n",
    "img_HWC[:, :, 1] = 1 - np.arange(0, 10000).reshape(100, 100) / 10000\n",
    "\n",
    "writer.add_image('my_image', img, 0)\n",
    "\n",
    "# If you have non-default dimension setting, set the dataformats argument.\n",
    "writer.add_image('my_image_HWC', img_HWC, 0, dataformats='HWC')\n",
    "writer.close()"
   ],
   "id": "2a6c1982d3e56618",
   "outputs": [],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:26:30.611848Z",
     "start_time": "2025-03-18T10:26:30.595428Z"
    }
   },
   "cell_type": "code",
   "source": [
    "img_batch = np.zeros((16, 3, 100, 100))\n",
    "for i in range(16):\n",
    "    img_batch[i, 0] = np.arange(0, 10000).reshape(100, 100) / 10000 / 16 * i\n",
    "    img_batch[i, 1] = (1 - np.arange(0, 10000).reshape(100, 100) / 10000) / 16 * i\n",
    "\n",
    "writer.add_images('my_image_batch', img_batch, 0)\n",
    "writer.close()"
   ],
   "id": "8aabad95ddc0fbb8",
   "outputs": [],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 6. Add Text",
   "id": "df6bc87e942a7e71"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:27:33.319447Z",
     "start_time": "2025-03-18T10:27:33.316773Z"
    }
   },
   "cell_type": "code",
   "source": [
    "writer.add_text('lstm', 'This is an lstm', 0)\n",
    "writer.add_text('rnn', 'This is an rnn', 10)"
   ],
   "id": "37b7f3b147df4074",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 7. Add embedding projector data to summary",
   "id": "2099cdff4a39b75"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:29:51.660827Z",
     "start_time": "2025-03-18T10:29:51.636009Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import keyword\n",
    "import torch\n",
    "\n",
    "meta = []\n",
    "while len(meta) < 100:\n",
    "    meta = meta + keyword.kwlist  # get some strings\n",
    "meta = meta[:100]\n",
    "\n",
    "for i, v in enumerate(meta):\n",
    "    meta[i] = v + str(i)\n",
    "\n",
    "label_img = torch.rand(100, 3, 10, 32)\n",
    "for i in range(100):\n",
    "    label_img[i] *= i / 100.0\n",
    "\n",
    "writer.add_embedding(torch.randn(100, 5), metadata=meta, label_img=label_img)\n",
    "writer.add_embedding(torch.randn(100, 5), label_img=label_img)\n",
    "writer.add_embedding(torch.randn(100, 5), metadata=meta)"
   ],
   "id": "8f220a0e7ca7ec06",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "warning: Embedding dir exists, did you set global_step for add_embedding()?\n",
      "warning: Embedding dir exists, did you set global_step for add_embedding()?\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 8. Add precision recall curve",
   "id": "ff76edd7afb37c80"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:31:25.422811Z",
     "start_time": "2025-03-18T10:31:25.419404Z"
    }
   },
   "cell_type": "code",
   "source": [
    "labels = np.random.randint(2, size=100)  # binary label\n",
    "predictions = np.random.rand(100)\n",
    "writer.add_pr_curve('pr_curve', labels, predictions, 0)\n",
    "writer.close()"
   ],
   "id": "440f8862339317e0",
   "outputs": [],
   "execution_count": 12
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 9. Add a set of hyperparameters to be compared in TensorBoard",
   "id": "94094cb168318862"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-18T10:34:16.314016Z",
     "start_time": "2025-03-18T10:34:16.306090Z"
    }
   },
   "cell_type": "code",
   "source": [
    "for i in range(5):\n",
    "    writer.add_hparams({'lr': 0.1 * i, 'bsize': i},\n",
    "                       {'hparam/accuracy': 10 * i, 'hparam/loss': 10 * i})"
   ],
   "id": "3349d5b30aee11fd",
   "outputs": [],
   "execution_count": 13
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
